﻿namespace PointerBinarySearch
{
    public unsafe class QuickArray
    {

        public int FindIndex(int value, params int[] array) 
        {
            //int* pTemp = stackalloc int[array.Length];
            //for (int i = 0; i < array.Length; i++)
            //{
            //    pTemp[i] = array[i];
            //}
            fixed (int* pTemp = array)
            {
                int* first, mid, last;
                first = pTemp;
                last = pTemp + array.Length - 1;
                mid = first + (last - first) / 2;

                while (first <= last)
                {
                    if (value < *mid)
                    {
                        last = mid - 1;
                    }
                    else if (value > *mid)
                    {
                        first = mid + 1;
                    }
                    else if (value == *mid)
                    {
                        return (int)(mid - pTemp);
                    }

                    mid = first + (last - first) / 2;
                }
                return *mid;
            }

        }

    }
}